package testClasses.misc;

import galaxies.starRegions.StarRegionBuilder;
import galaxies.starRegions.StarRegionModel;

import java.awt.Polygon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh.Mode;

import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.CustomMesh;
import utilities.Key;
import view.EViewList;
import view.View;
import controller.AbstractController;

public class GlobalStarRegionBuilderTest extends ATest {

	private static final Logger logger = Logger.getLogger(GlobalStarRegionBuilderTest.class.getName());
	
	public GlobalStarRegionBuilderTest(TestingSystem system) {
		super(system);
		this.name = "GlobalStarRegionBuilderTest";
	}

	@Override
	public boolean execute() {
		logger.log(Level.INFO, "Execute()");
		
		Random rand = new Random();
		
		HashMap<Key, AbstractController> controllerMap = new HashMap<Key, AbstractController>();
		eventManager.addControlMapReference(controllerMap);
		
		StarRegionBuilder builder = new StarRegionBuilder();
		
//		ArrayList<ArrayList<Integer>> dims = new ArrayList<ArrayList<Integer>>();
//		ArrayList<Integer> temp1 = new ArrayList<Integer>();
//		ArrayList<Integer> temp2 = new ArrayList<Integer>();
//		ArrayList<Integer> temp3 = new ArrayList<Integer>();
//		ArrayList<Integer> temp4 = new ArrayList<Integer>();
//		
//		temp1.add(5); temp1.add(0);
//		temp2.add(0); temp2.add(5);
//		temp3.add(-5); temp3.add(0);
//		temp4.add(0); temp4.add(-5);
		
//		dims.add(temp1);
//		dims.add(temp2);
//		dims.add(temp3);
//		dims.add(temp4);
		
		
		ArrayList<ArrayList<Integer>> dims = new ArrayList<ArrayList<Integer>>();
		
		//double longAxis = 1.0;
		//double shortAxis = 0.5+rand.nextDouble()*0.5;
		
		//ellipse, area = pi*a*b, where a, b are longDim, shortDim respectively 
		int longDim = 50;//(int) (totalTiles/(Math.PI*shortAxis));
		int shortDim = 200;//(int) (totalTiles/(Math.PI*longAxis));
		
		double resolution = 200;
		double theta = 0;
		double thetaStep = 2*Math.PI/resolution;
		
		int x, y;
		ArrayList<Integer> point;
		for (theta = 0; theta < 2*Math.PI; theta+=thetaStep) {
			point = new ArrayList<Integer>();
			x = (int) (longDim*Math.cos(theta));
			y = (int) (shortDim*Math.sin(theta));
			point.add(x);
			point.add(y);
			
			dims.add(point);
		}
		

		float x1;
		float y1;
		CustomMesh mesh1;
		Geometry geo1;
		Material mat1;
		int j = 0;
		
		mesh1 = new CustomMesh(4);
		mesh1.setMode(Mode.LineLoop);
		
//		if (j > colorList.size()-1) {
//			j = 0;
//		}
//		mesh1.setColor(colorList.get(j));		
		mesh1.setColor(ColorRGBA.Green);
		mesh1.setPointSize(5);
		
		for (ArrayList<Integer> poly: dims) {
			
			x1 = poly.get(0);
			y1 = poly.get(1);
			
			mesh1.addVertex(x1, y1, 0);
		}
		
		mesh1.finish();
		mesh1.setStatic();
		
		geo1 = new Geometry("points", mesh1);
		mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat1.setBoolean("VertexColor", true);
		//mat.setColor("Color", ColorRGBA.Green);
		geo1.setMaterial(mat1);
		node.attachChild(geo1);		
		
		
		ArrayList<StarRegionModel> regions = builder.buildRegions(dims);
		
		
		CustomMesh mesh2;
		Geometry geo2;
		Material mat2;
		
		mesh2 = new CustomMesh(4*regions.size());
		mesh2.setMode(Mode.Points);
		mesh2.setColor(ColorRGBA.Blue);
		mesh2.setPointSize(5);
		
		System.out.println("Regions size: "+ regions.size());
		
		for (int i = 0; i < regions.size(); i++) {
			LinkedHashMap<Integer, Vector2f> points = regions.get(i).getPoints();
			
			Vector2f pt1 = points.get(1);
			Vector2f pt2 = points.get(2);
			Vector2f pt3 = points.get(3);
			Vector2f pt4 = points.get(4);
			
			System.out.println("X,Y= " + pt1.x + " " + pt1.y);
			System.out.println("X,Y= " + pt2.x + " " + pt2.y);
			System.out.println("X,Y= " + pt3.x + " " + pt3.y);
			System.out.println("X,Y= " + pt4.x + " " + pt4.y);
			
			mesh2.addVertex(pt1.x, pt1.y, 0);
			mesh2.addVertex(pt2.x, pt2.y, 0);
			mesh2.addVertex(pt3.x, pt3.y, 0);
			mesh2.addVertex(pt4.x, pt4.y, 0);
			
//			for (Map.Entry<Integer, Vector2f> entry : points.entrySet()) {
//				Integer ref = entry.getKey();
//				Vector2f pt = entry.getValue();
//				
//				mesh2.addVertex(pt.x, pt.y, 0);				
//			}			
		}
		
		mesh2.finish();
		mesh2.setStatic();
		
		geo2 = new Geometry("points", mesh2);
		mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat2.setBoolean("VertexColor", true);
		//mat.setColor("Color", ColorRGBA.Green);
		geo2.setMaterial(mat2);
		node.attachChild(geo2);	
		
		
		
		
		
		
		return false;
	}
	
	

}
